import pandas as pd
import re
import requests

class NewsLabeler(object):
    def __init__(self,sentence):
        self.sentence = sentence
        self.url = "http://localhost:8000/predict.json"
        self.headers = {"Content-Type": "application/json"}
    
    def tokenize(self):
        pattern = "[^a-zA-Z]"
        return  re.sub(pattern, ' ', self.sentence)

    def labeler(self):
        token = self.tokenize()
        data = '{"text":"%s"}'%token
        response = requests.post(url = self.url,headers=self.headers,data=data.encode('utf-8'))
        labels = eval(response.text)["predictions"]["descriptors600"][:20]
        return labels
    
class TableLabeler(NewsLabeler):
    def __init__(self, target_df, term_column ,sentence_column):
        self.table = pd.DataFrame()
        self.columns = ["dbpedia word"]
        self.target_df = target_df
        self.term_column = term_column
        self.sentence_column = sentence_column
        
        self.column_init()
        self.table_init()
        
#         print(self.table)
        
    def column_init(self):
        for i in range(1,21):
            self.columns.append("Topic %s"%i)
            self.columns.append("Topic %s Score"%i)
    
    def table_init(self):
        columns = self.columns
        self.table = pd.DataFrame(columns = columns)
#         print(self.table.head())
    def insert_table(self, term, labels):
        df_row = [term]
        for label in labels:
            df_row.append(label['label'])
            df_row.append(label['score'])
        serie = pd.Series(df_row, index = self.columns)
#         print(serie)
#         print(self.table)
        self.table = self.table.append(serie, ignore_index=True)

    def single_process(self, term, sentence):
        label_list = NewsLabeler(sentence).labeler()
#         print(term)
        self.insert_table(term,label_list)
        
    def mult_process(self):
        for i, dp in self.target_df.iterrows():
            term = dp[self.term_column]
            sentence = dp[self.sentence_column]
#             print(term,sentence)
            self.single_process(term,sentence)
#   
        return self.table


# In[ ]:




